package com.tianyiyan.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtils
{
	private HibernateUtils(){} 
	
	private static HibernateUtils s_shareHibernateUtils = null;
	
	private static SessionFactory sessionFactory = null;
	
	public static HibernateUtils getInstance()
	{
		if (s_shareHibernateUtils == null)
			s_shareHibernateUtils = new HibernateUtils();
		return s_shareHibernateUtils;
	}
	
	public SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			Configuration configuration = new Configuration().configure();
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		}
		return sessionFactory;
	}
	
	public Session getSession(){
		return getSessionFactory().getCurrentSession();
	}
}
